﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UsersForms
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var pwd = textBox2.Text;

            if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")//判断是否输入内容
            {
                if (textBox2.Text == textBox3.Text)//判断两次输入的密码是否正确
                {
                    const string str = "^[A-Za-z0-9]+$";

                    if (Regex.IsMatch(pwd, str))
                    {
                        string sql = string.Format("select * from Users where Username='{0}' and Passwords='{1}'", name, pwd);

                        DataTable mytable = DBHelper.Select(sql);

                        var myrow = mytable.Rows.Count;

                        if (myrow == 1)//判断是否存在用户名和密码都相同的用户
                        {

                            MessageBox.Show("已存在用户名！！");
                        }
                        else
                        {
                            string sql2 = string.Format("insert into Users values('{0}','{1}')", name, pwd);

                            var n = DBHelper.DeleteUpdateInsert(sql2);

                            if (n >= 1)
                            {
                                MessageBox.Show("注册成功");
                                this.Close();
                            }
                            else
                            {
                                MessageBox.Show("注册失败");
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("密码只能为数字或者英文", "提示");
                        textBox2.Clear();
                        textBox3.Clear();
                    }
                }
                else
                {
                    MessageBox.Show("两次输入的密码不正确");

                    textBox2.Clear();
                    textBox3.Clear();
                }
            }
            else
            {
                MessageBox.Show("请填完全信息，谢谢");
            }
        }

        //取消
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
